﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using KLY.DataModels;
using KLY.Tools.Helper;

namespace KLY.ServiceCore
{
    public abstract class ServiceBase<T, R> 
        where T : class
        where R : class
    {
        public virtual R Process()
        {
            if (!Validate())
            {
                return null;
            }

            R response;

            if (Method == HttpMethod.POST)
            {
                response = HttpAgent.SendHttpPost<R>(Url, JsonHelper.Serialize(Request), ListHead);
            }
            else
            {
                response = HttpAgent.SendHttpGet<R>(Url, ListHead);
            }

            return response;
        }


        public virtual string ProcessApi()
        {
            if (!Validate())
            {
                return string.Empty;
            }

            string response = string.Empty;

            if (Method == HttpMethod.POST)
            {
                response = HttpAgent.SendHttpPost(Url, JsonHelper.Serialize(Request), ListHead);
            }
            else
            {
                response = HttpAgent.SendHttpGet(Url, ListHead);
            }

            return response;
        }
        #region protected

        protected string Url { get; set; }

        protected List<RequestHead> ListHead { get; set; }

        protected T Request { get; set; }

        protected HttpMethod Method { get; set; }

        #endregion

        private bool Validate()
        {
            bool ret = true;

            if (string.IsNullOrWhiteSpace(Url))
            {
                ret = false;
            }

            if (Method.GetType() != typeof(HttpMethod))
            {
                ret = false;
            }

            return ret;
        }
    }

    public enum HttpMethod
    {
        GET,
        POST
    }
}
